package com.meitianhui.convenitenbuy.module.home.presenter;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.Constants;
import com.meitianhui.convenitenbuy.base.BaseResponse;
import com.meitianhui.convenitenbuy.config.InfoProvider;
import com.meitianhui.convenitenbuy.http.ResponseObserver;
import com.meitianhui.convenitenbuy.module.home.contract.FreePaperContract;
import com.meitianhui.convenitenbuy.module.home.http.HomeApiDataSource;
import com.meitianhui.convenitenbuy.module.home.model.bean.FreePaper;
import com.meitianhui.convenitenbuy.module.home.ui.activity.FreePaperActivity;
import com.meitianhui.convenitenbuy.module.order.model.bean.EmptyInfo;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.util.HashMap;
import java.util.Map;

public class FreePaperPresenter implements FreePaperContract.Presenter {

    private FreePaperActivity mContext;
    private FreePaperContract.View mView;
    private final IWXAPI wxApi;

    public FreePaperPresenter(FreePaperContract.View view, FreePaperActivity context) {
        this.mView = view;
        this.mContext = context;
        wxApi = WXAPIFactory.createWXAPI(context, Constants.WX_APP_ID, true);
    }

    @Override
    public void getFreePaper(String pageIndex, String pageSize) {
        Map<String, String> maps = new HashMap<>();
        maps.put("page_no", pageIndex + "");
        maps.put("page_size", pageSize + "");
        String pageJson = new Gson().toJson(maps);

        Map<String, String> map = new HashMap<>();
        map.put("member_id", InfoProvider.getUserToken(App.getInstance()).getMember_id());
        String paramJson = new Gson().toJson(map);

        HomeApiDataSource.getInstance()
                .getFreePaper(InfoProvider.getUserToken(App.getInstance()).getUser_token(),
                        "gdBenefit.consumer.gdBenefitListPageFind", pageJson, paramJson)
                .subscribe(new ResponseObserver<BaseResponse<FreePaper>>(mContext, true) {
                    @Override
                    public void onSuccess(BaseResponse<FreePaper> response) {
                        mView.getFreePaper(response.data);
                    }
                });
    }

    @Override
    public void joinFightGroupPay(String activityId, String benefitId, String price) {
        Map<String, String> params = new HashMap<>();
        params.put("data_source", Constants.CUSTOM_APP);
        params.put("member_id", InfoProvider.getUserToken(App.getInstance()).getMember_id());
        params.put("member_type_key", "consumer");
        params.put("activity_id", activityId);
        String mobile = InfoProvider.getUserInfo(App.getInstance()).getMobile();
        if (!TextUtils.isEmpty(mobile)) {
            params.put("member_mobile", mobile);
        }
        params.put("benefit_id", benefitId);
        params.put("amount", price);
        String paramJson = new Gson().toJson(params);

        HomeApiDataSource.getInstance()
                .freeCouponPay(InfoProvider.getUserToken(App.getInstance()).getUser_token(),
                        "tsOrder.consumer.freeCouponPay", paramJson)
                .subscribe(new ResponseObserver<BaseResponse<EmptyInfo>>(mContext, true) {
                    @Override
                    public void onSuccess(BaseResponse<EmptyInfo> response) {
                        mView.joinFightGroupPaySuccess();
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        mView.joinFightGroupPayFail(code, msg);
                    }
                });
    }

    @Override
    public void attachView(Object view) {
    }

    @Override
    public void detachView() {
    }

}
